/*
 * Created by JFormDesigner on Thu Apr 14 22:39:46 CST 2022
 */

package com.ZengQi.event.view;

import java.awt.event.*;
import com.ZengQi.event.controller.UserSearchViewController;
import com.ZengQi.event.entity.Topic;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author QI Zeng
 */
public class UserSearchView extends JFrame {
    private String topicName;
    private String topicPublisher;
    private String topicContent;
    private UserMainView userMainView;
    private List<Topic>list=null;
    private Topic thisTopic=null;

    /**
     * 用户的搜题界面
     * @param userMainView
     */
    public UserSearchView(UserMainView userMainView) {
        this.userMainView=userMainView;
        UserSearchViewController userSearchViewController=new UserSearchViewController();
        initComponents();
        Topic conditionTopic=new Topic();
        conditionTopic.setTopicName("%"+userMainView.getSearchTxt().getText()+"%");
        list=userSearchViewController.topicList(conditionTopic);
        if(list!=null){
            int i=0;
            DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
            dtm.setRowCount(0);
            for(Topic topic:list){
                i=i+1;
                Vector<String> vector=new Vector<String>();
                vector.add(topic.getTopicName());
                vector.add(topic.getTopicPublisher());
                vector.add(topic.getTopicContent());
                dtm.addRow(vector);
            }
        }else{
            JOptionPane.showMessageDialog(userMainView,"未找到相关题目！");
            new UserMainView(userMainView.getUserNameTxt().getText());
            return;




        }

        setVisible(true);
        setLocationRelativeTo(null);

    }

    private void table1MousePressed(MouseEvent e) {
        tableMousePressed(e);
    }

    private void tableMousePressed(MouseEvent e) {
        int row=table1.getSelectedRow();
        Topic topic=list.get(row);
        thisTopic=list.get(row);
        topicNameTxt.setText(topic.getTopicName());
        topicPublisherTxt.setText(topic.getTopicPublisher());
        topicContentArea.setText(topic.getTopicContent());

        topicName=topic.getTopicName();
        topicPublisher=topic.getTopicPublisher();
        topicContent=topic.getTopicContent();
    }

    private void lookCommentBtn(ActionEvent e) {
        lookCommentBtnActionPerformed(e);
    }

    private void lookCommentBtnActionPerformed(ActionEvent e) {
        new LookCommentView(topicName);
    }

    private void addBtn(ActionEvent e) {
        addBtnActionPerformed(e);
    }

    private void addBtnActionPerformed(ActionEvent e) {
       new AddCommentView(userMainView.getUserNameTxt().getText(),topicName);
       dispose();
    }

    private void deleteBtn(ActionEvent e) {
        deleteBtnActionPerformed(e);
    }

    private void deleteBtnActionPerformed(ActionEvent e) {
        if(thisTopic==null){
            JOptionPane.showMessageDialog(this,"请选择要删除的评论所在题目！！");

        }
        else {
            new RemoveCommentView(topicName, userMainView.getUserNameTxt().getText());
        }
        }

    private void gotoAnswer(ActionEvent e) {
        gotoAnswerActionPerformed(e);
    }

    private void gotoAnswerActionPerformed(ActionEvent e) {
        Topic conditionTopic=new Topic();
        conditionTopic.setTopicName(topicName);
        conditionTopic.setTopicContent(topicContent);
        new AnswerTopicView(conditionTopic,userMainView.getUserNameTxt().getText());
    }

    /**
     * 组件布局及初始化
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel1 = new JPanel();
        label1 = new JLabel();
        topicNameTxt = new JTextField();
        label2 = new JLabel();
        topicPublisherTxt = new JTextField();
        label3 = new JLabel();
        scrollPane2 = new JScrollPane();
        topicContentArea = new JTextArea();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();

        //======== this ========
        setTitle("\u505a\u9898\u5e2e");
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u9898\u76ee\u540d\u79f0", "\u51fa\u9898\u4eba", "\u9898\u76ee\u5185\u5bb9"
                }
            ) {
                boolean[] columnEditable = new boolean[] {
                    false, false, false
                };
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return columnEditable[columnIndex];
                }
            });
            {
                TableColumnModel cm = table1.getColumnModel();
                cm.getColumn(0).setResizable(false);
                cm.getColumn(0).setPreferredWidth(220);
                cm.getColumn(1).setResizable(false);
                cm.getColumn(1).setPreferredWidth(175);
                cm.getColumn(2).setResizable(false);
                cm.getColumn(2).setPreferredWidth(285);
            }
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    table1MousePressed(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //======== panel1 ========
        {

            //---- label1 ----
            label1.setText("\u9898\u76ee\u540d\u79f0");

            //---- topicNameTxt ----
            topicNameTxt.setEditable(false);

            //---- label2 ----
            label2.setText("\u51fa\u9898\u4eba");

            //---- topicPublisherTxt ----
            topicPublisherTxt.setEditable(false);

            //---- label3 ----
            label3.setText("\u5185\u5bb9");

            //======== scrollPane2 ========
            {

                //---- topicContentArea ----
                topicContentArea.setEditable(false);
                scrollPane2.setViewportView(topicContentArea);
            }

            //---- button1 ----
            button1.setText("\u67e5\u770b\u8bc4\u8bba");
            button1.addActionListener(e -> lookCommentBtn(e));

            //---- button2 ----
            button2.setText("\u6dfb\u52a0\u8bc4\u8bba");
            button2.addActionListener(e -> addBtn(e));

            //---- button3 ----
            button3.setText("\u5220\u9664\u8bc4\u8bba");
            button3.addActionListener(e -> deleteBtn(e));

            //---- button4 ----
            button4.setText("\u7b54\u9898");
            button4.addActionListener(e -> gotoAnswer(e));

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(panel1Layout.createParallelGroup()
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
                                        .addGap(26, 26, 26)
                                        .addComponent(topicNameTxt, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addGroup(panel1Layout.createParallelGroup()
                                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                                            .addGroup(panel1Layout.createSequentialGroup()
                                                .addGap(41, 41, 41)
                                                .addComponent(label3)))
                                        .addGroup(panel1Layout.createParallelGroup()
                                            .addGroup(panel1Layout.createSequentialGroup()
                                                .addGap(18, 18, 18)
                                                .addComponent(topicPublisherTxt, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE))
                                            .addGroup(panel1Layout.createSequentialGroup()
                                                .addGap(5, 5, 5)
                                                .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 378, GroupLayout.PREFERRED_SIZE)
                                                .addGap(84, 84, 84)
                                                .addComponent(button4))))))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(119, 119, 119)
                                .addComponent(button1)
                                .addGap(61, 61, 61)
                                .addComponent(button2)
                                .addGap(80, 80, 80)
                                .addComponent(button3)))
                        .addContainerGap(43, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label1)
                            .addComponent(topicNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label2)
                            .addComponent(topicPublisherTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(label3)
                            .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                            .addComponent(button4, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(button1)
                            .addComponent(button2)
                            .addComponent(button3))
                        .addGap(35, 35, 35))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap(54, Short.MAX_VALUE)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 657, GroupLayout.PREFERRED_SIZE)
                    .addGap(32, 32, 32))
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(18, 18, 18)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel1;
    private JLabel label1;
    private JTextField topicNameTxt;
    private JLabel label2;
    private JTextField topicPublisherTxt;
    private JLabel label3;
    private JScrollPane scrollPane2;
    private JTextArea topicContentArea;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
